<template>
	<view class="pages">
		<view class="main">
			<view class="cover_photograph" :style="{backgroundImage:'url('+formData.cover+')'}">
				<view class="change_cover u-flex-xy-center" @click="change_cover">
					<text>更改封面</text>
				</view>
			</view>
			<u--form labelPosition="left" :model="model" ref="form1" style="margin-bottom:16rpx;">
				<u-form-item label="" labelWidth="auto" prop="userInfo.name"
					:borderBottom='index==filter_list.length-1?false:true' ref="item1"
					v-for="(item,index) in filter_list" :key="index">
					<yy-screen  v-model="formData[item.fieldName]" ref="child"
						:config='item'>
					</yy-screen>
				</u-form-item>
			</u--form>
		</view>
		
			<view class="cell-group u-border-radius" v-for="item in information_list">
				<yy-uploadpictures v-model="formData[item.fieldName]" ref="child" @change_list="change_list"
					:config='item'>
					<view>
						<u--textarea v-model="formData.desc" placeholder="用照片和祝福展示幸福时刻，记录幸福时刻，分享幸 福时刻" border="none" placeholderStyle="{color:'#B3B1B2';font-size:'28rpx'}" height="140"></u--textarea>
						
					</view>
				</yy-uploadpictures>
			</view>
		
		<view class="submit" @click="submit" v-if="id==0">
			<text>发起活动</text>
		</view>
		<view class="submit" v-else @click="edit_Activity">
			<text>完成</text>
		</view>
	</view>
</template>

<script>
	import COS from 'cos-js-sdk-v5';
	import cos from '@/common/cos.js'
	const app = getApp();
	import {
		addActivity,
		getActivityOne,
		editActivity
	} from '@/common/api.js'
	export default {
		data() {
			return {
				autoHeight:true,
				$http_cos:app.$http_cos,
				config: {
					title: '红娘上传美图',
					long_title: '',
					fieldName: '',
					id: 3,
					array: [],
					maxCount: 1,
					width: '622',
					height: '340',

				},
				information_list: [{
					title: '活动介绍',
					fieldName: 'descImage',
					field: 'chunk',
					id: 3,
					maxCount: 1,
					array: [],
					width: '202',
					height: '202',
					slot:true,
				}, {
					title: '活动瞬间',
					fieldName: 'moment',
					id: 4,
					field: 'chunk',
					maxCount: 6,
					array: [],
					width: '202',
					height: '202',
				}],
				model: {},
				filter_list: [{
					title: '活动名称',
					long_title: '输入活动名称，最多14个字',
					modality: 'input',
					name: '活动名称',
					fieldName: 'name',
					is_show: false,
					is_audit: 1,
					rightIcon: true,
					array: []
				}, {
					title: '活动人数',
					modality: 'input',
					name: '活动人数',
					fieldName: 'num',
					is_show: false,
					is_audit: 1,
					is_audit: 2,
					rightIcon: true,
				}, {
					title: '活动时间',
					modality: 'datetime',
					name: '活动时间',
					fieldName:'time',
					is_show: false,
					is_audit: 1,
					defaultIndex: [0, 0, 0],
					is_audit: 2,
					maxDate:19,
					minDate:20
					
				}, {
					title: '活动地点',
					modality: 'input',
					name: '活动地点',
					fieldName: 'address',
					is_show: false,
					is_audit: 1,
					defaultIndex: [0, 0, 0],
					is_audit: 2,
					rightIcon: true,
					
				}],
				
				formData: {
					cover: '',
					name: '',
					address: '',
					num: 0,
					time:'',
					desc: '',
					descImage: '[]',
					moment:'[]'
				},
				id:0
			}
		},
		onLoad(option) {
			if(option.id){
				this.id=option.id
				this.get_ActivityList(option.id)
			}
		},
		watch: {
			formData: {
				handler(value) {
					console.log('深度监听', value)
				},
				deep: true // 深度监听
			},
		},
		methods: {
			edit_Activity(){
				uni.showLoading({
					title: '修改中'
				});
				editActivity(this.formData, {
					custom: {
						auth: false,
						toast: false,
						catch: false
					}
				}).then((result) => {
						uni.$u.toast('修改成功')
						uni.switchTab({
							url:'/pages/moment/index'
						})
					// this.return_beforePage()
					
				}).catch((err) => {
					uni.hideLoading();
				})
			},
			get_ActivityList(id) {
				let data = {
					params: {
						id: id
					}
			
				}
				getActivityOne(data, {
					custom: {
						auth: false,
						toast: false,
						catch: false
					}
				}).then((result) => {
					
					this.formData = result
				}).catch((err) => {
					
				})
			},
			pickExclude(obj, keys) {
				// 某些情况下，type可能会为
			    if (!['[object Object]', '[object File]'].includes(Object.prototype.toString.call(obj))) {
			        return {}
			    }
			    return Object.keys(obj).reduce((prev, key) => {
			        if (!keys.includes(key)) {
			            prev[key] = obj[key]
			        }
			        return prev
			    }, {})
			},
			formatImage(res) {
			    return res.tempFiles.map((item) => ({
			        ...this.pickExclude(item, ['path']),
			        type: 'image',
			        url: item.path,
			        thumb: item.path,
					size: item.size,
					// #ifdef H5
					name: item.name
					// #endif
			    }))
			},
			chooseImage(){
				return new Promise((resolve, reject) => {
				        uni.chooseImage({
				            count: 1,
				            success: (res) => resolve(this.formatImage(res)),
				            fail: reject
				        })
				})
			},
			async change_cover(){
				let list=[]
					let tempFiles=await  this.chooseImage()
					for(let i=0;i<tempFiles.length;i++){
						let name = tempFiles[i].name
						let index = tempFiles[i].name.indexOf('.')
						let name1 = name.substring(0, index)
						let name2 = name.substring(index, name.length)
						tempFiles[i].name = name1 + cos.uuid() + name2
						const blob = await cos.objectURLToBlob(tempFiles[i].url, tempFiles[i].name)
						 const result = await cos.uploadFilePromise(blob, tempFiles[i].name)
						list.push('https://'+result+'?imageView2/2/w/750/h/q/85')
					}
					this.formData.cover=list.toString()
			},
			return_beforePage(){
				let pages = getCurrentPages()
				if(pages.length>0){
					let beforePage = pages[pages.length - 2]
					// #ifdef H5
					beforePage.init_info()
					// #endif
					// #ifndef H5
					beforePage.$vm.init_info()
					// #endif
					uni.navigateBack()
				}else{
					uni.switchTab({
						url:'/pages/moment/index'
					})
				}
			},
			submit() {
				uni.showLoading({
					title: '发布中'
				});
				addActivity(this.formData, {
					custom: {
						auth: true,
						toast: false,
						catch: true
					}
				}).then((r) => {
					uni.$u.toast('发布成功')
					this.return_beforePage()
				}).catch((err) => {
					console.log(err)
					uni.hideLoading();
				})
			}

		}
	}
</script>

<style lang="scss" scoped>
	.pages {
		padding: 24rpx;
		box-sizing: border-box;

		.main {
			background-color: #fff;
			padding: 24rpx 24rpx 0;
			box-sizing: border-box;
			border-radius: 20rpx;
			/deep/ .shop_title{
				margin-bottom: 32rpx;
			}
			/deep/ .u-textarea{
				padding:0 18rpx 18rpx;
				font-size:28rpx;
			}
			.cover_photograph {
				width: 654rpx;
				height: 300rpx;
				background-color: #ADADAD;
				position: relative;
				background-size: 100% 100%;
				background-repeat: no-repeat;
				background-position:center center;  
				.change_cover {
					position: absolute;
					right: 24rpx;
					top: 24rpx;
					width: 140rpx;
					height: 52rpx;
					background-color: #000000;
					text-align: center;
					line-height: 52rpx;
					opacity: 0.3;

					text {
						font-size: 26rpx;
						font-weight: 400;
						color: #fff;
					}
				}
			}

			/deep/ .u-form-item__body {
				padding: 0;
			}
		}
		.cell-group {
			width:100%;
			background-color: #fff;
			border-radius:16rpx;
		}
		.submit {
			width: 620rpx;
			height: 92rpx;
			background: #FF7612;
			border-radius: 46rpx;
			color: #fff;
			text-align: center;
			line-height: 92rpx;
			margin: auto;
			box-sizing: border-box;

			text {
				font-size: 28rpx;
			}
		}
	}
</style>
